package cn.itcast.reggie.controller;

import cn.itcast.reggie.common.R;
import cn.itcast.reggie.dto.DishDto;
import cn.itcast.reggie.pojo.Category;
import cn.itcast.reggie.pojo.Dish;
import cn.itcast.reggie.pojo.DishFlavor;
import cn.itcast.reggie.service.CategoryService;
import cn.itcast.reggie.service.DishFlavorService;
import cn.itcast.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishFlavorService dishFlavorService;



    @PostMapping
    public R add(@RequestBody DishDto dishDto) {
        dishService.add(dishDto);
        return R.success("添加成功");
    }

   /* @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name) {
        IPage<Dish> pageInfo = new Page<>(page,pageSize);
        QueryWrapper<Dish> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(name)) {
            wrapper.like("name",name);
        }

        IPage<Dish> result = dishService.page(pageInfo, wrapper);
        return R.success(result);
    }
*/
   @GetMapping("/page")
   public R page(Integer page,Integer pageSize,String name) {
       IPage<Dish> pageInfo = new Page<>(page,pageSize);
       QueryWrapper<Dish> wrapper = new QueryWrapper<>();
       if(StringUtils.isNotEmpty(name)) {
           wrapper.like("name",name);
       }
       IPage<Dish> result = dishService.page(pageInfo, wrapper);
       List<Dish> records = result.getRecords(); //获取当前页数据
       List<Dish> dishDtos = new ArrayList<>(); //存放DishDTO的集合

       for (Dish dish : records) {
           //将dish中属性copy到dto中,只有属性名和类型相同属性才能copy
           DishDto dishDto = new DishDto();
           BeanUtils.copyProperties(dish,dishDto);

           //根据菜品中分类id查询分类对象
           Category category = categoryService.getById(dish.getCategoryId());
           dishDto.setCategoryName(category.getName());
           dishDtos.add(dishDto);
       }
       result.setRecords(dishDtos);
       return R.success(result);
   }

   @GetMapping("/{id}")
   public R findById(@PathVariable("id") Long id) {
       Dish dish = dishService.getById(id);
       QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
       wrapper.eq("dish_id",id);
       List<DishFlavor> flavors = dishFlavorService.list(wrapper);

       DishDto dishDto = new DishDto();
       BeanUtils.copyProperties(dish,dishDto);
       dishDto.setFlavors(flavors);
       return R.success(dishDto);
   }

   @PutMapping
   public R update(@RequestBody DishDto dishDto) {
       dishService.updateById(dishDto);

       QueryWrapper<DishFlavor> wrapper = new QueryWrapper<>();
       wrapper.eq("dish_id",dishDto.getId());
       dishFlavorService.remove(wrapper);

       List<DishFlavor> flavors = dishDto.getFlavors();
       for (DishFlavor flavor : flavors) {
           flavor.setDishId(dishDto.getId());
           dishFlavorService.save(flavor);
       }
       return R.success("修改成功");
   }


   @GetMapping("/list")
   //使用pojo类型接受前端传递菜品分类id
   public R list(Dish dish) {
        //根据菜品分类id查询菜品
       QueryWrapper<Dish> wrapper = new QueryWrapper<>();

       wrapper.eq("category_id",dish.getCategoryId());
       wrapper.eq("status",1);
       wrapper.orderByDesc("update_time");
       List<Dish> list = dishService.list(wrapper);
       //改造代码
       //迭代菜品集合获取每个菜品
       List<DishDto> dtoList = new ArrayList<>();
       for (Dish d : list) {
           //根据菜品id查询菜品口味
           QueryWrapper<DishFlavor> queryWrapper = new QueryWrapper<>();
           queryWrapper.eq("dish_id",d.getId());
           List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);
           //将菜品信息封装到Dto
           DishDto dishDto = new DishDto();
           BeanUtils.copyProperties(d,dishDto);
           //将菜品口味封装到dto
           dishDto.setFlavors(dishFlavorList);
           //根据id查询分类对象
           Long categoryId = d.getCategoryId();//分类id
           Category category = categoryService.getById(categoryId);
           String categoryName = category.getName();
           dishDto.setCategoryName(categoryName);
           dtoList.add(dishDto);
       }

       return R.success(dtoList);
   }

}
